<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Cities;
use App\Models\FileUpload;
use App\Models\FilmTypeMaster;
use App\Models\MacroManagement;
use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\PartMaster;
use App\Models\ProvinceMaster;
use App\Models\RegionMaster;
use App\Services\ModuleService;
use Encore\Admin\Grid\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use PhpOffice\PhpSpreadsheet\IOFactory;

class SettingModelController extends Controller
{
    public function index(Request $request)
    {
        $admin_id = session('admin.id');
        /*if ($admin_id == 1) {
            $u = [];
            foreach ($u as $k => $v) {
                $model = Admin::findOrNew($k);
                $model->id = $k;
                $name = $v[0];
                $password = $v[1];
                $phone_number = $v[2];
                $model->username = $name;
                $model->abbr = $name;
                $model->unique_id = $name;
                $model->company_name = $name;
                $model->first_name = $name;
                $model->user_type = 1;
                $model->status = 1;
                $model->phone_number = $phone_number;
                $model->password = bcrypt($password);
                $model->save();
            }
            return [$u];
        }*/

        checkAuthority();
        $page = [
            'page_name' => 'model',
            'title' => ts('model'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = DB::table('model_master as m')
            ->whereNull('m.deleted_at')
            ->leftJoin('make_master as k', 'm.make_id', 'k.id');
        $year_id = $request->input('year_id');
        if ($year_id) {
            $model = $model->where('k.year_id', $year_id);
        }
        $make_id = $request->input('make_id');
        if ($make_id) {
            $model = $model->where('m.make_id', $make_id);
        }
        $model = filterByColumns($model, ['m.english_value', 'm.traditional_chiness_value', 'm.simplified_chiness_value']);

        $model = filterByDateRange($model, 'm.created_at');
        $status = $request->input('status');
        if ($status !== null) {
            $model = $model->where('m.status', $status);
        }
        $menus = (new ModuleService())->getWebButton(session('admin.role_id') ?? 0, $request->path());

        $data = $model->select(['m.id', 'm.english_value', 'm.traditional_chiness_value', 'm.simplified_chiness_value', 'm.make_id', 'k.year_id'])
            ->orderByDesc('id')
            ->paginate(50);
        return view('admin.model.index', [
            'data'=> $data,
            'page' => $page,
            'menus' => $menus,
        ]);
    }

    public function create(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'model',
            'title' => ts('addnew'),
            'breadcrumb' => [
                [
                    'text' => ts('model'),
                    'url' => '/admin/setting/model/'
                ]
            ],
        ];
        $data = new ModelMaster();
        return view('admin.model.create', [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function store(Request $request)
    {
        checkAuthority();
        $model = new ModelMaster();
        $model->make_id = (int) $request->input('make_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->status = 1;
        $model->save();
        makeLangFiles();
        return redirect('/admin/setting/model')->with('trash', '新增成功！');
    }

    public function edit(Request $request, $id)
    {
        checkAuthority();
        $page = [
            'page_name' => 'model',
            'title' => ts('edit'),
            'breadcrumb' => [
                [
                    'text' => ts('model'),
                    'url' => '/admin/setting/model/'
                ]
            ],
        ];
        $data = ModelMaster::findOrFail($id);
        return view('admin.model.edit', [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function update(Request $request, $id)
    {
        checkAuthority();
        $model = ModelMaster::findOrFail($id);
        $model->make_id = (int) $request->input('make_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->status = 1;
        $model->save();
        makeLangFiles();
        return redirect('/admin/setting/model')->with('trash', '更新成功！');
    }

    public function import(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'model',
            'title' => ts('tongbu'),
            'breadcrumb' => [
                [
                    'text' => ts('model'),
                    'url' => '/admin/setting/model'
                ]
            ],
        ];
        return view('admin.model.import', [
            'page' => $page,
        ]);
    }
    /*
     * 省代同步操作
     * 仅根据unique_id查询，不再根据判断其他内容判断*/
    public function handle_import(Request $request)
    {
        checkAuthority();
        $admin_id = session('admin.id');
        $file = $request->file('file');
        if (empty($file)) {
            return back()->withInput()->with('trash', [ 'content' => '文件不能为空！', 'type' => 'error']);
        }
        $ext = $file->getClientOriginalExtension();
        $allow_type = [
            'csv','xls','xlsx'
        ];
        if (!in_array($ext, $allow_type)) {
            return back()->with('trash', [ 'msg' => '不支持的文件类型！', 'type' => 'error']);
        }

        $filename = Str::random(16) . '.xlsx';
        $path = getFileStorePath(2);
        $bool = $file->storeAs($path['path'], $filename, 'storage');
        $full_file_name = $path['path'].$filename;
        $mod = new FileUpload();
        $mod->type = 2;
        $mod->name = '车型同步';
        $mod->user_id = $admin_id;
        $mod->file = $full_file_name;
        $mod->save();
        $pfile = $path['base'] . $filename;
        if (file_exists($pfile)) {
            $spreadsheet = IOFactory::load($file);
            $sheet = $spreadsheet->getActiveSheet();
            $row_no = 1;
            $n = 2;
            $data = [];
            while ($n){
                $item = [];
                $n = strtoupper(trim($sheet->getCell('A'.$row_no)->getValue()));
                if ($n) {
                    foreach (range('A', 'C') as $v) {
                        $item[$v] = trim($sheet->getCell($v.$row_no)->getValue());
                    }
                    $data[] = $item;
                }
                $row_no++;
            }
        }
        $makes = MakeMaster::withTrashed()->get();
        $make_en = $make_zh_tw = $make_zh = [];
        foreach ($makes as $k => $v) {
            $ken = $v->english_value.'###'.$v->year_id;
            $ktw = $v->traditional_chiness_value.'###'.$v->year_id;
            $kzh = $v->simplified_chiness_value.'###'.$v->year_id;
            $make_en[$ken] = $v->id;
            $make_tw[$ktw] = $v->id;
            $make_zh[$kzh] = $v->id;
        }

        $models = ModelMaster::withTrashed()->get();
        $model_en = $model_zh_tw = $model_zh = [];
        foreach ($models as $k => $v) {
            $ken = $v->english_value.'###'.$v->make_id;
            $ktw = $v->traditional_chiness_value.'###'.$v->make_id;
            $kzh = $v->simplified_chiness_value.'###'.$v->make_id;
            $model_en[$ken] = $v->id;
            $model_tw[$ktw] = $v->id;
            $model_zh[$kzh] = $v->id;
        }
        /*
         * */
        $upload = [];
        $upk = [];
        foreach ($data as $k => $v) {
            $make_name = trim($v['A']);
            $model_name = trim($v['B']);
            $year_id = (int) trim($v['C']);
            $uk = $make_name.'###'.$model_name.'###'.$year_id;
            $upk[$uk] = @$upk[$uk] + 1;
            if (empty($year_id) || empty($make_name)) {
                continue;
            }
            $key = $make_name.'###'.$year_id;
            if (!empty($make_zh[$key])) {
                $make_id = $make_zh[$key];
                // $upload[] = $make_zh[$key];
            } elseif (!empty($make_tw[$key])) {
                // $upload[] = $make_tw[$key];
                $make_id = $make_tw[$key];
            } elseif (!empty($make_en[$key])) {
                // $upload[] = $make_en[$key];
                $make_id = $make_en[$key];
            } else {
                $upload[] = 'xxx';
                $makeModel = new MakeMaster();
                $makeModel->year_id = $year_id;
                $makeModel->english_value = $make_name;
                $makeModel->traditional_chiness_value = $make_name;
                $makeModel->simplified_chiness_value = $make_name;
                $makeModel->save();
                $make_zh[$key] = $makeModel->id;
                $make_tw[$key] = $makeModel->id;
                $make_en[$key] = $makeModel->id;
                $make_id = $makeModel->id;
            }
            $model_key = $model_name.'###'.$make_id;
            if (!empty($model_zh[$model_key])) {
                $model_id = $model_zh[$model_key];
            } elseif (!empty($model_tw[$model_key])) {
                $model_id = $model_tw[$model_key];
            } elseif (!empty($model_en[$model_key])) {
                $model_id = $model_en[$model_key];
            } else {
                // $upload[] = $model_key;
                $modelModel = new ModelMaster();
                $modelModel->make_id = $make_id;
                $modelModel->simplified_chiness_value = trim($modelModel->simplified_chiness_value) ?: $model_name;
                $modelModel->traditional_chiness_value = trim($modelModel->simplified_chiness_value) ?: $model_name;
                $modelModel->english_value = trim($modelModel->simplified_chiness_value) ?: $model_name;
                $modelModel->save();
            }

            /*$modelModel = ModelMaster::where('make_id', $make_id)->where(function ($query) use ($model_name) {
                $query->where('simplified_chiness_value', 'like', $model_name)
                    ->orWhere('traditional_chiness_value', 'like', $model_name)
                    ->orWhere('english_value', 'like', $model_name);
            })->first();
            if (empty($modelModel->id)) {
                $modelModel = new ModelMaster();
            }
            $modelModel->make_id = $make_id;
            $modelModel->simplified_chiness_value = trim($modelModel->simplified_chiness_value) ?: $model_name;
            $modelModel->traditional_chiness_value = trim($modelModel->simplified_chiness_value) ?: $model_name;
            $modelModel->english_value = trim($modelModel->simplified_chiness_value) ?: $model_name;
            $modelModel->save();*/
        }
        // return [$upload, count($data), count($upk)];

        return redirect('/admin/setting/model');
    }

    public function destroy(Request $request, $id)
    {
        $model = ModelMaster::findOrFail($id);
        $model->delete();
        return fs200('deleteSuccess');
    }



    public function view_cities(Request $request)
    {
        $page = [
            'page_name' => 'cities',
            'title' => ts('CITY'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = $this->view_list($request, 'cities');
        return view($this->view_index, [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function edit_cities(Request $request, $id)
    {
        $page = [
            'page_name' => 'cities',
            'title' => ts('CITY'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = Cities::findOrFail($id);
        return view($this->view_edit, [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_cities(Request $request, $id)
    {
        $page = [
            'page_name' => 'cities',
            'title' => ts('CITY'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = Cities::findOrFail($id);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/cities')->with('trash', 'sad');
    }

    public function view_province(Request $request)
    {
        $page = [
            'page_name' => 'province',
            'title' => ts('PROVINCE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = $this->view_list($request, 'province');
        return view($this->view_index, [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_province(Request $request, $id)
    {
        $page = [
            'page_name' => 'province',
            'title' => ts('PROVINCE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = ProvinceMaster::findOrFail($id);
        return view($this->view_edit, [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_province(Request $request, $id)
    {
        $model = ProvinceMaster::findOrFail($id);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/province')->with('trash', 'sad');
    }

    public function view_region(Request $request)
    {
        $page = [
            'page_name' => 'region',
            'title' => ts('REGION'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = $this->view_list($request, 'region');
        return view($this->view_index, [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function edit_region(Request $request, $id)
    {
        $page = [
            'page_name' => 'region',
            'title' => ts('REGION'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = RegionMaster::findOrFail($id);
        return view($this->view_edit, [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_region(Request $request, $id)
    {
        $model = RegionMaster::findOrFail($id);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/region')->with('trash', 'sad');
    }

    public function macro_management_index(Request $request)
    {
        $page = [
            'page_name' => 'macro_management',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new MacroManagement();
        $model = filterByColumns($model, ['macro_name', 'english_value', 'traditional_chiness_value', 'simplified_chiness_value']);
        $data = $model->orderByDesc('id')->paginate(50);
        return view('admin.admin.macro-management-index', [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function view_macro_management(Request $request)
    {
        $page = [
            'page_name' => 'macro_management',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = $this->view_list($request, 'macro_management');
        return view($this->view_index, [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function edit_macro_management(Request $request, $id)
    {
        $page = [
            'page_name' => 'macro_management',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = MacroManagement::findOrFail($id);
        return view('admin.setting.edit_macro_management', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_macro_management(Request $request, $id)
    {
        $model = MacroManagement::findOrFail($id);
        $macro_name = str_replace(' ', '', trim(strtoupper(trim($request->input('macro_name')))));
        $macro_name = str_replace(' ', '', $macro_name);
        $model->macro_name = trim(strtoupper($model->macro_name));
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        makeLangFile('macro_management');
        return redirect('/admin/setting/macro_management')->with('trash', 'sad');
    }
    public function create_macro_management(Request $request)
    {
        $page = [
            'page_name' => 'macro_management',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new MacroManagement;
        return view('admin.setting.create_macro_management', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_macro_management(Request $request)
    {
        $model = new MacroManagement;
        $macro_name = str_replace(' ', '', trim(strtoupper(trim($request->input('macro_name')))));
        $macro_name = str_replace(' ', '', $macro_name);
        $model->macro_name = trim($macro_name);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        makeLangFile('macro_management');
        return redirect('/admin/setting/macro_management')->with('trash', 'sad');
    }

    // filmtype
    public function view_filmtype(Request $request)
    {
        $page = [
            'page_name' => 'filmtype',
            'title' => ts('FILMTYPE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new FilmTypeMaster;
        $product_types = (int) $request->input('product_types');
        if ($product_types) {
            $model = $model->where('product_type_id', $product_types);
        }

        $data = $this->view_list_by_model($request, $model);
        return view('admin.setting.view_filmtype', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_filmtype(Request $request, $id)
    {
        $page = [
            'page_name' => 'filmtype',
            'title' => ts('FILMTYPE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = FilmTypeMaster::findOrFail($id);
        return view('admin.setting.edit_filmtype', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_filmtype(Request $request, $id)
    {
        $model = FilmTypeMaster::findOrFail($id);
        $model->product_type_id = (int) $request->input('product_type_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/filmtype')->with('trash', ts('FILMTYPE') . ' ' . ts('UPDATESUCCESS'));
    }
    public function create_filmtype(Request $request)
    {
        $page = [
            'page_name' => 'filmtype',
            'title' => ts('FILMTYPE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new FilmTypeMaster;
        return view('admin.setting.create_filmtype', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_filmtype(Request $request)
    {
        $model = new FilmTypeMaster();
        $model->product_type_id = (int) $request->input('product_type_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/filmtype')->with('trash', ts('FILMTYPE') . ' ' . ts('ADDSUCCESS'));
    }
    public function delete_filmtype(Request $request, $id)
    {
        $model = FilmTypeMaster::findOrFail($id);
        $model->delete();
        return ['status'=>'SUCCESS', 'msg' => ts('FILMTYPE') . ' ' . ts('DELETESUCCESS')];
    }


    // make
    public function view_make(Request $request)
    {
        $page = [
            'page_name' => 'make',
            'title' => ts('MAKE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new MakeMaster;
        $year_id = (int) $request->input('year');
        if ($year_id) {
            $model = $model->where('year_id', $year_id);
        }
        $data = $this->view_list_by_model($request, $model);
        return view('admin.setting.view_make', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_make(Request $request, $id)
    {
        $page = [
            'page_name' => 'make',
            'title' => ts('MAKE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = MakeMaster::findOrFail($id);
        return view('admin.setting.edit_make', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_make(Request $request, $id)
    {
        $model = MakeMaster::findOrFail($id);
        $model->year_id = (int) $request->input('year_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/make')->with('trash', ts('MAKE') . ' ' . ts('UPDATESUCCESS'));
    }
    public function create_make(Request $request)
    {
        $page = [
            'page_name' => 'make',
            'title' => ts('MAKE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new MakeMaster;
        return view('admin.setting.create_make', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_make(Request $request)
    {
        $model = new MakeMaster();
        $model->year_id = (int) $request->input('year_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/make')->with('trash', ts('MAKE') . ' ' . ts('ADDSUCCESS'));
    }
    public function delete_make(Request $request, $id)
    {
        $model = MakeMaster::findOrFail($id);
        $model->delete();
        return ['status'=>'SUCCESS', 'msg' => ts('MAKE') . ' ' . ts('DELETESUCCESS')];
    }

    // model
    public function view_model(Request $request)
    {
        $page = [
            'page_name' => 'model',
            'title' => ts('MODEL'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new ModelMaster;
        $make_id = (int) $request->input('make_id');
        if ($make_id) {
            $model = $model->where('make_id', $make_id);
        }
        $data = $this->view_list_by_model($request, $model);
        return view('admin.setting.view_model', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_model(Request $request, $id)
    {
        $page = [
            'page_name' => 'model',
            'title' => ts('MODEL'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = ModelMaster::findOrFail($id);
        return view('admin.setting.edit_model', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_model(Request $request, $id)
    {
        $model = ModelMaster::findOrFail($id);
        $model->make_id = (int) $request->input('make_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/model')->with('trash', ts('MODEL') . ' ' . ts('UPDATESUCCESS'));
    }
    public function create_model(Request $request)
    {
        $page = [
            'page_name' => 'model',
            'title' => ts('MODEL'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new ModelMaster;
        return view('admin.setting.create_model', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_model(Request $request)
    {
        $model = new ModelMaster();
        $model->make_id = (int) $request->input('make_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/model')->with('trash', ts('MODEL') . ' ' . ts('ADDSUCCESS'));
    }
    public function delete_model(Request $request, $id)
    {
        $model = ModelMaster::findOrFail($id);
        $model->delete();
        return ['status'=>'SUCCESS', 'msg' => ts('MODEL') . ' ' . ts('DELETESUCCESS')];
    }

    // part
    public function view_part(Request $request)
    {
        $page = [
            'page_name' => 'part',
            'title' => ts('PART'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new PartMaster;
        $year_id = (int) $request->input('year');
        if ($year_id) {
            $model = $model->where('year_id', $year_id);
        }
        $data = $this->view_list_by_model($request, $model);
        return view('admin.setting.view_part', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_part(Request $request, $id)
    {
        $page = [
            'page_name' => 'part',
            'title' => ts('PART'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = PartMaster::findOrFail($id);
        return view('admin.setting.edit_part', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_part(Request $request, $id)
    {
        $model = PartMaster::findOrFail($id);
        /*$model->year_id = (int) $request->input('year_id');*/
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->min_length = (int) $request->input('min_length');
        $model->save();
        $url = $request->input('_previous_') ?: '/admin/setting/part';
        return redirect($url)->with('trash', ts('PART') . ' ' . ts('UPDATESUCCESS'));
    }
    public function create_part(Request $request)
    {
        $page = [
            'page_name' => 'part',
            'title' => ts('PART'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new PartMaster;
        return view('admin.setting.create_part', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_part(Request $request)
    {
        $model = new PartMaster();
        /*$model->year_id = (int) $request->input('year_id');*/
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/part')->with('trash', ts('PART') . ' ' . ts('ADDSUCCESS'));
    }
    public function delete_part(Request $request, $id)
    {
        $model = PartMaster::findOrFail($id);
        $model->delete();
        return ['status'=>'SUCCESS', 'msg' => ts('PART') . ' ' . ts('DELETESUCCESS')];
    }


    public function view_list(Request $request, $model)
    {
        if ($model == 'cities') {
            $model = new Cities();
        } elseif ($model == 'province') {
            $model = new ProvinceMaster();
        } elseif ($model == 'region') {
            $model = new RegionMaster();
        } elseif ($model == 'macro_management') {
            $model = new MacroManagement();
        } else {
            return false;
        }

        $label = $request->input('label');
        $value = $request->input('value');
        if (!empty($value)) {
            if ($label == 1) {
                $model = $model->where('english_value', 'like', '%'.$value.'%');
            } elseif ($label == 2) {
                $model = $model->where('traditional_chiness_value', 'like', '%'.$value.'%');
            } elseif ($label == 2) {
                $model = $model->where('simplified_chiness_value', 'like', '%'.$value.'%');
            } else {
                $model = $model->orWhere('english_value', 'like', '%'.$value.'%')
                    ->orWhere('traditional_chiness_value', 'like', '%'.$value.'%')
                    ->orWhere('simplified_chiness_value', 'like', '%'.$value.'%');
            }
        }
        $data = $model->orderByDesc('id')->paginate(50);
        return $data;
    }

    public function view_list_by_model(Request $request, $model)
    {
        $label = $request->input('label');
        $value = $request->input('value');
        if (!empty($value)) {
            $model = filterByColumns($model, ['english_value', 'traditional_chiness_value', 'simplified_chiness_value']);
            /*if ($label == 1) {
                $model = $model->where('english_value', 'like', '%'.$value.'%');
            } elseif ($label == 2) {
                $model = $model->where('traditional_chiness_value', 'like', '%'.$value.'%');
            } elseif ($label == 2) {
                $model = $model->where('simplified_chiness_value', 'like', '%'.$value.'%');
            } else {
                $model = $model->orWhere('english_value', 'like', '%'.$value.'%')
                    ->orWhere('traditional_chiness_value', 'like', '%'.$value.'%')
                    ->orWhere('simplified_chiness_value', 'like', '%'.$value.'%');
            }*/
        }

        $data = $model->orderBy('id')->paginate();
        return $data;
    }

    public function getModels(Request $request)
    {
        $make_name = $request->input('make_name');
        return DB::table('model_master')
            ->leftJoin('make_master', 'make_master.id', 'model_master.make_id')
            ->where('make_master.simplified_chiness_value', $make_name)
            ->whereNull('make_master.deleted_at')
            ->whereNull('model_master.deleted_at')
            ->select('model_master.*')
            ->groupBy('model_master.simplified_chiness_value')
            ->get();
    }
}
